<template>
  <div id="home">
    <nav-bar class="nav-bar">
      <div slot="center" class="nav-bar-center">购物街</div>
    </nav-bar>
    <tab-control
      :titles="['流行', '新款', '精选']"
      @tabClick="tabClick"
      ref="tabControl1"
      class="tabShow"
      v-show="isTabShow"
    />
    <scroll
      class="wrapper"
      ref="scroll"
      :probeType="2"
      @scroll="contentScroll"
      :pullUpLoad="true"
      @pullingUp="loadMore"
    >
      <home-swiper
        :banners="banners"
        class="home-swiper"
        @imgLoad="swiperImgLoad"
      />
      <recommend-view :recommends="recommends" />
      <feature-view />
      <tab-control
        :titles="['流行', '新款', '精选']"
        @tabClick="tabClick"
        ref="tabControl2"
      />
      <goods-list :goodsList="showGoods" />
    </scroll>
    <back-top
      class="back-top"
      @click.native="backTopClick"
      v-show="isShowBackTop"
    />
  </div>
</template>

<script>
import NavBar from "components/common/navbar/NavBar.vue";
import HomeSwiper from "./childComps/HomeSwiper.vue";
import RecommendView from "./childComps/RecommendView.vue";
import FeatureView from "./childComps/FeatureView.vue";
import TabControl from "components/content/tabControl/TabControl.vue";
import GoodsList from "components/content/goods/GoodsList.vue";
import Scroll from "components/common/scroll/Scroll.vue";

import { getHomeMultiData, getHomeGoods } from "network/home.js";
import { itemListenerMixin, backTop } from "common/mixin";

export default {
  name: "Home",
  mixins: [itemListenerMixin, backTop],
  data() {
    return {
      banners: [],
      recommends: [],
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      currentGoodsType: "pop",
      tabOffsetTop: 0,
      isTabShow: false,
      saveY: 0,
    };
  },
  components: {
    NavBar,
    HomeSwiper,
    RecommendView,
    FeatureView,
    TabControl,
    GoodsList,
    Scroll,
  },
  created() {
    this.getHomeMultiData();
    this.getHomeGoods("pop");
    this.getHomeGoods("new");
    this.getHomeGoods("sell");
  },
  methods: {
    // 1. 网络请求相关
    getHomeMultiData() {
      getHomeMultiData().then((res) => {
        this.banners = res.data.banner.list;
        this.recommends = res.data.recommend.list;
      });
    },
    getHomeGoods(type) {
      // 初始化页码为1
      const page = this.goods[type].page + 1;
      getHomeGoods(type, page).then((res) => {
        this.goods[type].list.push(...res.data.list);
        // 调用一次getHomeGoods()，page加一
        this.goods[type].page += 1;
        this.$refs.scroll.finishPullUp();
      });
    },

    // 事件监听相关方法
    tabClick(index) {
      switch (index) {
        case 0:
          this.currentGoodsType = "pop";
          break;
        case 1:
          this.currentGoodsType = "new";
          break;
        case 2:
          this.currentGoodsType = "sell";
          break;
        default:
          break;
      }
      this.$refs.tabControl1.currentIndex = index;
      this.$refs.tabControl2.currentIndex = index;
    },
    // backTop() {
    //   this.$refs.scroll.scrollTo(0, 0, 500);
    // },
    contentScroll(position) {
      this.isShowBackTop = -position.y > 1500;
      this.isTabShow = -position.y > this.tabOffsetTop;
    },
    loadMore() {
      this.getHomeGoods(this.currentGoodsType);
    },
    swiperImgLoad() {
      // console.log(this.$refs.tabControl.$el.offsetTop);
      this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop;
    },
  },
  computed: {
    showGoods() {
      return this.goods[this.currentGoodsType].list;
    },
  },
  mounted() {},
  activated() {
    // 当组件处于活跃状态时，跳转到不活跃时滑动的位置
    this.$refs.scroll.scrollTo(0, this.saveY, 500);
    // this.$refs.scroll.refresh();
    // 可以直接使用混入
    this.refresh();
  },
  deactivated() {
    // 记录组件活跃状态滑动的位置
    this.saveY = this.$refs.scroll.getScrollY();
    // 取消事件总线的监听
    this.$bus.$off("imgLoaded", this.itemImgListener);
  },
};
</script>

<style scoped>
#home {
  height: 100vh;
  position: relative;
}
.nav-bar {
  /* position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 2; */
  background-color: var(--color-tint);
}
.nav-bar-center {
  color: #fff;
  text-align: center;
}

.wrapper {
  overflow: hidden;
  position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
}
.back-top {
  position: fixed;
  bottom: 70px;
  right: 20px;
}

.tabShow {
  position: relative;
  z-index: 9;
  background-color: #fff;
}
</style>
